package com.poi;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

public class CreateExcel {
	
	
	/**
	 * POI生成Excel文件
	 * @throws Exception
	 */
	@Test
	public void createExcelforPOI() throws Exception {
		//创建Excel工作薄
		HSSFWorkbook workbook=new HSSFWorkbook();
		//创建一个工作表
		HSSFSheet sheet = workbook.createSheet();
		//创建第一行
		HSSFRow row=sheet.createRow(0);
		HSSFCell cell=null;
		
		//表头
		String[] title={"id","name","sex"};
		for(int i=0;i<title.length;i++){
			cell=row.createCell(i);
			cell.setCellValue(title[i]);
		}
		
		//追加数据
		for(int i=1;i<=10;i++){
			HSSFRow nextrow = sheet.createRow(i);
			HSSFCell newcell = nextrow.createCell(0);
			newcell.setCellValue("a"+i);
			newcell = nextrow.createCell(1);
			newcell.setCellValue("狗娃"+i);
			newcell = nextrow.createCell(2);
			newcell.setCellValue("男"+i);
		}
		//创建一个文件
		File file=new File("src/muke.xls");
		try {
			file.createNewFile();
			//将Excel内容存盘
			FileOutputStream stream = FileUtils.openOutputStream(file);
			workbook.write(stream);
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	

}
